﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace GameBase.Form.Control
{
    public class MyTextBox : MyControl
    {
        private Sprite pointerSprite;
        private int pointerIndex;
        private double backTime=0;
        //private Keys lastKey;
        int maxLength = 18;

        public int MaxLength
        {
            get { return maxLength; }
            set { maxLength = value; }
        }
        
        public int PointerIndex
        {
            get { return pointerIndex; }
            set { pointerIndex = value; }
        }
        public MyTextBox(String strFilePath, Vector2 position, string text)
        {
            initButton(strFilePath, position, text);
        }
        public MyTextBox(Vector2 position, string text)
        {
            initButton(@"System/textbox", position, text);
        }
        public MyTextBox(Vector2 position)
        {
            initButton(@"System/textbox", position, "");
        }
        private void initButton(String strFilePath, Vector2 position, string text)
        {
            textAlign = Align.Left;
            sprite = new GameBase.Sprite(strFilePath, 3, 1);
            Position = position;
            Text = text;
            font = GameBase.Constant.content.Load<SpriteFont>(@"Resources/fontNormal");
            pointerSprite = new Sprite(@"System/textPointer", 1, 2, new List<int> {0,1}, 300);
            pointerSprite.SetOriginPosition(ORIGIN_POSITION.MidTop);
         
        }
        public override void ControlUpdate(GameTime gameTime)
        {
            base.ControlUpdate(gameTime);
            pointerSprite.Update(gameTime);
            bool hasKey = true;
            if (Keyboard.GetState().IsKeyDown(Keys.Back))
            {
                backTime += gameTime.ElapsedGameTime.TotalMilliseconds;
                if (backTime > 50 && pointerIndex > 0)
                {
                    pointerIndex--;
                    text = text.Substring(0, pointerIndex) + text.Substring(pointerIndex+1);
                    backTime -= 50;
                }

            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                backTime += gameTime.ElapsedGameTime.TotalMilliseconds;
                if (backTime > 50 && pointerIndex > 0)
                {
                    pointerIndex--;
                    backTime -= 50;
                }

            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                backTime += gameTime.ElapsedGameTime.TotalMilliseconds;
                if (backTime > 50 && pointerIndex < text.Length)
                {
                    pointerIndex++;
                    backTime -= 50;
                }

            }
            else
            {
                backTime = 0;
                hasKey = false;
            }
            if (hasKey)
            {
                
            }
            
        }
        public override void KeyPressed(List<Keys> pressedKey)
        {
            
            base.KeyPressed(pressedKey);
            if (text.Length >= maxLength)
            {
                return;
            }
            foreach (Keys key in pressedKey)
            {
                string keyText = key.ToString();
                if (keyText.Length > 1)
                {
                    char lastChar = keyText[keyText.Length - 1];
                    if (lastChar >= '0' && lastChar <= '9')
                    {
                        keyText = lastChar.ToString();
                    }
                    else
                    {
                        return;
                    }
                }
                if (Keyboard.GetState().IsKeyUp(Keys.LeftShift)
                    && Keyboard.GetState().IsKeyUp(Keys.RightShift))
                {
                    keyText = keyText.ToLower();
                }

                text = text.Substring(0, pointerIndex) + keyText + text.Substring(pointerIndex);
                pointerIndex++;
                
            }
        }
        public override void MouseReleased(int x, int y)
        {
            base.MouseReleased(x, y);
            pointerIndex = text.Length;
        }
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            base.Draw(gameTime, spriteBatch);

            if (isForcused)
            {
                Vector2 textSize = font.MeasureString(text.Substring(0,PointerIndex));
                Vector2 pos = GetTextPosition();
                pos.X += textSize.X;
                pos.Y = bound.Y;
                pointerSprite.Draw(gameTime, spriteBatch,
                pos, screenScale, 0);
            }
        }
    }
}
